<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\models\{User, HallTrade as HallTrades, Config, Hall as Halls};
use think\Facade\Config as Configs;
use think\Db;
use app\facade\Redis;
use think\facade\Debug;


class HallTrade extends Command
{
    private $url = 'https://api-aws.huobi.pro/';

    protected function configure()
    {
        $this->setName('HallTrade')->setDescription('自动完成');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        $cmd = "ps aux | grep '/www/server/php/72/bin/php /www/wwwroot/JTE/think Jtequotes'|grep -v 'grep'| awk '{print $2}'";
        exec($cmd, $ps, $ret);
        if (count($ps) > 1) {
            die("batch of  is running");
        }
        ini_set('max_execution_time', 0);
        //未付款自动取消
        $paycacel = (new Config())->where('key_value', 'paycacel')->value('value') ?? 30;
        $time = date('Y-m-d H:i:s', time() - 60 * $paycacel);
        $list = (new HallTrades())->where(['status' => 0])->where('created', '>', date('Y-m-d', time() - 100 * 86400))->where('created', '<=', $time)->select();
        foreach ($list as $trade) {
            // $hall = (new Halls())->where('id', $trade->hall_id)->find();
            $selluser = (new User())->where('id', $trade['seller_id'])->find();
            Db::startTrans();
            try {
                (new User())->where('id', $trade['buyer_id'])->setDec('reput', 1);
                (new HallTrades())->where('id', $trade['id'])
                    ->where(['status' => 0])
                    ->update([
                        'status' => 10, 'caceltime' => date('Y-m-d H:i:s')
                    ]);
                (new User())->MoneyEdit($selluser, 3, 7, ($trade['num'] + $trade['handfee']), '交易售卖' . $trade['num'] . '超时自动取消', $trade['id'], $trade['handfee'], 0);
                Db::commit();
                // $this->checkHall($trade->hall_id);
            } catch (\Exception $exception) {
                Db::rollback();
                echo $exception->getMessage();
            }
            echo '取消操作成功';
        }
        //未确认自动确认
        $finishtime = (new Config())->where('key_value', 'finishtrue')->value('value') ?? 120;
        $ftime = date('Y-m-d H:i:s', time() - 60 * $finishtime);
        $list2 = (new HallTrades())->where(['status' => 1])->where('paytime', '>', date('Y-m-d', time() - 100 * 86400))->where('paytime', '<=', $ftime)->select();
        foreach ($list2 as $trade) {
            $buyuser = (new User())->where('id', $trade['buyer_id'])->find();
            Db::startTrans();
            try {
                (new HallTrades())->where('id', $trade['id'])
                    ->where('status', 1)
                    ->update([
                        'status' => 2,
                        'finishtime' => date('Y-m-d H:i:s')
                    ]);
                (new User())->MoneyEdit($buyuser, 3, 6, $trade['num'], '交易购买确认', $trade['id'], 0, 0);
                Db::commit();
                // $this->checkHall($trade->hall_id);
            } catch (\Exception $exception) {
                Db::rollback();
                echo $exception->getMessage();
            }
            echo '确认操作成功';
        }
    }


    // protected function checkHall($id)
    // {
    //     $hall = (new Halls())->where('id', $id)->find();
    //     $results = (new HallTrades())
    //         ->where('hall_id', $id)
    //         ->where('status', 'in', '0,1,8,9')
    //         ->field('id')
    //         ->select();
    //     if (count($results) > 0 && $hall['status'] == 1 && $hall['surplus'] > 0) {
    //         if ($hall['type'] == 2) {
    //             (new Halls())->where('id', $id)->update([
    //                 'status' => 9,
    //                 'deleted' => date('Y-m-d H:i:s'),
    //             ]);
    //         } else {
    //             (new Halls())->where('id', $id)->update(['status' => 0]);
    //         }
    //     }
    //     if (count($results) <= 0 && $hall['surplus'] == 0) {
    //         (new Halls())->where('id', $id)->update(['status' => 2]);
    //     }
    //     return true;
    // }
}
